package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.P2PHBService;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONBuilder;

/*
 * @author fan
 * @className P2PHBController
 * @description p2p红包明细控制器
 * @date 2016年1月5日上午10:07:46
 */
@Controller
@RequestMapping("/HB")
public class P2PHBController extends JsonController {
	@Autowired
	private P2PHBService p2phbService;

	/**
	 * @方法名称:hBDetailsQuery
	 * @方法描述:红包明细查询
	 * @author fan
	 * @date 2016年1月5日上午10:09:39
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/hBDetailsQuery")
	public ModelAndView hBDetailsQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String USER_ID = RequestUtils.getStringParameter(request, "USER_ID");
		String HB_USE_DATE_START = RequestUtils.getStringParameter(request, "HB_USE_DATE_START");
		String HB_USE_DATE_END = RequestUtils.getStringParameter(request, "HB_USE_DATE_END");
		String HB_USE_CONDITION_MIN_JE = RequestUtils.getStringParameter(request, "HB_USE_CONDITION_MIN_JE");
		String HB_STATE = RequestUtils.getStringParameter(request, "HB_STATE");
		String HB_EFFECTIVE_DAYS = RequestUtils.getStringParameter(request, "HB_EFFECTIVE_DAYS");
		String HB_JE = RequestUtils.getStringParameter(request, "HB_JE");
		String HB_CREATE_DATE_START = RequestUtils.getStringParameter(request, "HB_CREATE_DATE_START");
		String HB_CREATE_DATE_END = RequestUtils.getStringParameter(request, "HB_CREATE_DATE_END");
		String USER_ID_LXFS = RequestUtils.getStringParameter(request,"USER_ID_LXFS");
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject parms = new JSONObject();
		parms.put("USER_ID", USER_ID);
		parms.put("HB_USE_DATE_START", HB_USE_DATE_START);
		parms.put("HB_USE_DATE_END", HB_USE_DATE_END);
		parms.put("HB_USE_CONDITION_MIN_JE", HB_USE_CONDITION_MIN_JE);
		parms.put("HB_STATE", HB_STATE);
		parms.put("HB_EFFECTIVE_DAYS", HB_EFFECTIVE_DAYS);
		parms.put("HB_JE", HB_JE);
		parms.put("HB_CREATE_DATE_START", HB_CREATE_DATE_START);
		parms.put("HB_CREATE_DATE_END", HB_CREATE_DATE_END);
		parms.put("USER_ID_LXFS", USER_ID_LXFS);
		parms.put("start",start);
		parms.put("limit", limit);
		List list = p2phbService.hbDetailsQuery(parms);
		JSONObject result = new JSONObject();
		result.put("data", list);
		int total = p2phbService.getPageCount(parms);
		result.put("total", total);
		if (null!=page) {//新版本,传参
			int totalPage=total%limit==0?total/limit:total/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @方法名称:hbgltzrInfos
	 * @方法描述:红包使用关联投资人信息
	 * @author fan
	 * @date 2016年1月13日下午3:14:58
	 * @return
	 */
	@RequestMapping("/hbsygltzrxx")
	public ModelAndView hbgltzrInfos(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String gltzrId = RequestUtils.getStringParameter(request, "REF_TZRXX_ID");
		JSONObject parms = new JSONObject();
		parms.put("id", gltzrId);
		List list =p2phbService.getGltzrxx(parms);
		JSONObject result = new JSONObject();
		result.put("data", list);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@RequestMapping("exportHBExcel")
	public ModelAndView exportHBExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String USER_ID = RequestUtils.getStringParameter(request, "USER_ID");
		String HB_USE_DATE_START = RequestUtils.getStringParameter(request, "HB_USE_DATE_START");
		String HB_USE_DATE_END = RequestUtils.getStringParameter(request, "HB_USE_DATE_END");
		String HB_USE_CONDITION_MIN_JE = RequestUtils.getStringParameter(request, "HB_USE_CONDITION_MIN_JE");
		String HB_STATE = RequestUtils.getStringParameter(request, "HB_STATE");
		String HB_EFFECTIVE_DAYS = RequestUtils.getStringParameter(request, "HB_EFFECTIVE_DAYS");
		String HB_JE = RequestUtils.getStringParameter(request, "HB_JE");
		String HB_CREATE_DATE_START = RequestUtils.getStringParameter(request, "HB_CREATE_DATE_START");
		String HB_CREATE_DATE_END = RequestUtils.getStringParameter(request, "HB_CREATE_DATE_END");
		String USER_ID_LXFS = RequestUtils.getStringParameter(request,"USER_ID_LXFS");
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		JSONObject parms = new JSONObject();
		parms.put("USER_ID", USER_ID);
		parms.put("HB_USE_DATE_START", HB_USE_DATE_START);
		parms.put("HB_USE_DATE_END", HB_USE_DATE_END);
		parms.put("HB_USE_CONDITION_MIN_JE", HB_USE_CONDITION_MIN_JE);
		parms.put("HB_STATE", HB_STATE);
		parms.put("HB_EFFECTIVE_DAYS", HB_EFFECTIVE_DAYS);
		parms.put("HB_JE", HB_JE);
		parms.put("HB_CREATE_DATE_START", HB_CREATE_DATE_START);
		parms.put("HB_CREATE_DATE_END", HB_CREATE_DATE_END);
		parms.put("USER_ID_LXFS", USER_ID_LXFS);
		parms.put("start",start);
		parms.put("limit", limit);
		// 查询数据
		List<Map<String, Object>> list = p2phbService.hbDetailsQuery(parms);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getHbExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"红包明细");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "红包明细.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
